package com.yzg.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzg.constants.HttpStatus;
import com.yzg.exceptionhandler.ServiceException;
import com.yzg.pojo.ProductCategory;
import com.yzg.service.ProductCategoryService;
import com.yzg.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: decade
 * @Description: 商品分类控制层
 * @Date: 2021/1/9 17:50
 **/
@Api(tags = "ProductCategoryController", description = "商品分类管理")
@RestController
@RequestMapping("/pms/productCate/")
@CrossOrigin
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;


    @PostMapping("doLogin")
    @ApiOperation(value = "登录")
    public R login(){
        return R.ok().data("token","admin");
    }


    @GetMapping("getInfo")
    @ApiOperation(value = "获取用户信息")
    public R getInfo(){
        return R.ok().data("roles","[admin]").data("name","admin").data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    }


    @PostMapping("doLogout")
    @ApiOperation(value = "退出登录")
    public R logout(){
        return R.ok();
    }


    @GetMapping("treeList/{page}/{pageSize}")
    @ApiOperation(value = "查询商品分类树形列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,paramType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "当前页大小",required = true,paramType = "Integer")
    })
    public R pageTreeList(@PathVariable Integer page,@PathVariable Integer pageSize){
        return R.ok().data("treeList",productCategoryService.pageTreeList());
    }


    @PutMapping("update/navStatus")
    @ApiOperation(value = "修改导航栏状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "导航栏状态数组id",required = true,paramType = "List<Long>"),
            @ApiImplicitParam(name = "navStatus",value = "导航栏状态",required = true,paramType = "Integer")
    })
    public R updateNavStatus(@RequestParam("ids") List<Long> ids, @RequestParam("navStatus") Integer navStatus){
        int row = productCategoryService.updateNavStatus(ids,navStatus);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PutMapping("update/showStatus")
    @ApiOperation(value = "修改显示状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "显示状态数组id",required = true,paramType = "List<Long>"),
            @ApiImplicitParam(name = "showStatus",value = "显示状态",required = true,paramType = "Integer")
    })
    public R updateShowStatus(@RequestParam("ids") List<Long> ids, @RequestParam("showStatus") Integer showStatus){
        int row = productCategoryService.updateShowStatus(ids,showStatus);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据id获取商品分类")
    @ApiImplicitParam(name = "id",value = "商品分类id",required = true,paramType = "Long")
    public R getProductCateById(@PathVariable Long id){
        ProductCategory productCategory = productCategoryService.getProductCateById(id);
        if(productCategory==null){
            throw new ServiceException(HttpStatus.BAD_REQUEST,"没有记录");
        }
        return R.ok().data("productCate",productCategory);
    }

    @PutMapping("update")
    @ApiOperation(value = "修改商品分类")
    @ApiImplicitParam(name = "productCate",value = "商品分类信息",required = true,paramType = "ProductCategory")
    public R update(@RequestBody ProductCategory productCate){
        System.out.println(productCate);
        int row = productCategoryService.updateProductCate(productCate);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "删除商品分类")
    @ApiImplicitParam(name = "id",value = "商品分类id",required = true,paramType = "Long")
    public R delete(@PathVariable Long id){
        int row = productCategoryService.deleteById(id);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping("add")
    @ApiOperation(value = "添加商品分类")
    @ApiImplicitParam(name = "productCate",value = "商品分类信息",required = true,paramType = "ProductCategory")
    public R add(@RequestBody ProductCategory productCate){
        int row = productCategoryService.add(productCate);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }
}
